package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult pageSelectSetmeal(SetmealPageQueryDTO dto) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);

        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        List<Setmeal> setmeals = setmealMapper.pageSelectSetmeal(setmeal);
        Page<SetmealVO> setmealVOS = new Page<>();
        setmeals.forEach(a -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(a, setmealVO);
            Category category = categoryMapper.selectCategoryById(setmealVO.getCategoryId());
            setmealVO.setCategoryName(category.getName());
            setmealVOS.add(setmealVO);
        });

        return new PageResult(setmealVOS.getTotal(), setmealVOS.getResult());
    }

    @Override
    public SetmealVO selectSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.selectSetmealById(id);

        List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(a -> {
            a.setSetmealId(setmeal.getId());
            Dish dish = dishMapper.selectDishById(a.getDishId());
            if (dish.getStatus() == StatusConstant.DISABLE) {
                throw new BaseException(MessageConstant.SETMEAL_INCLUDES_DISH_NO_SALE);
            }
            //redisTemplate.delete("user_" + dish.getCategoryId());
        });
        setmealDishMapper.insertSetmealDish(setmealDishes);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!setmealDishes.isEmpty()) {
            setmealDishMapper.deleteSetmealDish(setmeal.getId());
            setmealDishes.forEach(a -> {
                a.setSetmealId(setmeal.getId());
                Dish dish = dishMapper.selectDishById(a.getDishId());
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new BaseException(MessageConstant.SETMEAL_INCLUDES_DISH_NO_SALE);
                }
                //redisTemplate.delete("user_" + dish.getCategoryId());
            });
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void statusStartOrStop(Integer status, Long id) {
        List<Dish> dish = dishMapper.selectDishBySetmeal(id);
        dish.forEach(a -> {
            if (a.getStatus() == StatusConstant.DISABLE) {
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });
        setmealMapper.updateSetmeal(Setmeal.builder().status(status).id(id).build());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void deleteSetmeal(List<Long> ids) {
        ids.forEach(a -> {
                    Dish dish = dishMapper.selectDishById(a);
                    if (dish != null) {
                        if (dish.getStatus() == StatusConstant.ENABLE) {
                            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_INCLUDES_DISH_ON_SALE);
                        }
                    }
                    Setmeal setmeal = setmealMapper.selectSetmealById(a);
                    if (setmeal.getStatus() == StatusConstant.ENABLE) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                    }
                    setmealDishMapper.deleteSetmealDish(a);
                    //redisTemplate.delete("user_" + dish.getCategoryId());
                }
        );
        setmealMapper.deleteSetmeal(ids);

    }

    @Override
    public List<Setmeal> selectSetmealByCategoryId(Long categoryId) {

        List<Setmeal> setmeals = setmealMapper.selectSetmealByCategoryId(categoryId);


        return setmeals;
    }

    @Override
    public List<DishItemVO> selectDishBySetmealId(Long id) {
        return setmealMapper.selectDishBySetmealId(id);
    }


}
